package day3;

import jdk.nashorn.internal.ir.JumpStatement;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

//测试序列化和反序列化
public class TestSerializable {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //output();
        input();
    }
    //序列化
    public static void output() throws IOException {
        Student s = new Student();
        s.setName("邓志涛");
        s.setStuId("007");
        s.setScore(60);
        StudentInfo info = new StudentInfo();
        info.setStuId("007");
        info.setSex("男");
        info.setPhone("23345678");
        s.setInfo(info);
        //因为序列化是转换成字节序列 所以文件的内容不能修改
        //所以本地文件会设置一个 无法打开的后缀名
        FileOutputStream fos = new FileOutputStream("d://IO//student.xxx");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(s);

    }
    //反序列化
    public static void input() throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream("d://IO//student.xxx");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student s = (Student) ois.readObject();
        System.out.println(s.getName()+" "+s.getStuId()+" "+s.getScore()+" "+s.getInfo().getPhone());
    }
}
class Student implements Serializable {
    //序列化还会默认的唯一标识：作用用于检测序列化和反序列化是否是同一个ID
    private static final long serialVersionUID = 3119357676l;
    private String stuId;//学号
    //忽略序列化 属性的值不保存
    private static String name;//姓名
    //忽略序列化 属性的值不保存
    private transient double score;//分数
    private StudentInfo info;

    public StudentInfo getInfo() {
        return info;
    }

    public void setInfo(StudentInfo info) {
        this.info = info;
    }

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}
class StudentInfo implements Serializable{
    private String stuId;
    private String sex;
    private String phone;

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}